package cn.gene.timeyan.ams.webapi.controller;

import cn.gene.timeyan.ams.service.IAdvertiseService;
import cn.gene.timeyan.common.restful.JsonPage;
import cn.gene.timeyan.common.restful.JsonResult;
import cn.gene.timeyan.pojo.admin.dto.AdvertiseAddDTO;
import cn.gene.timeyan.pojo.admin.dto.AdvertiseUpdateDTO;
import cn.gene.timeyan.pojo.admin.vo.AdvertiseVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 广告   前端控制器*
 *
 * @author LWG
 * @date 2022/7/21 16:49
 * @since 1.0.0@gene.cn
 */
@RestController
@RequestMapping("/ams/admin/advertise")
@Api(tags = "广告模块")
@Slf4j
public class AdvertiseController {
    private final IAdvertiseService advertiseService;

    public AdvertiseController(IAdvertiseService advertiseService) {
        this.advertiseService = advertiseService;
    }

    @PostMapping("/insertAD")
    @ApiOperation("插入广告的方法")
    public JsonResult<Void> insertAD(AdvertiseAddDTO advertiseAddDTO){
        advertiseService.addAD(advertiseAddDTO);
        return JsonResult.ok();
    }

    @PostMapping("/upload")
    @ApiOperation("处理图片的方法,返回图片路径")
    public JsonResult<String> handlePicture(MultipartFile picFile){
        String imgUrl = advertiseService.handlePicture(picFile);
        return JsonResult.ok(imgUrl);
    }

    @PostMapping("/delete/{adId}")
    @ApiOperation("删除广告的方法")
    public JsonResult<Void> delete(@PathVariable Long adId){
        advertiseService.deleteAD(adId);
        return JsonResult.ok();
    }

    @PostMapping("/remove")
    @ApiOperation("移除被删除广告对应的本地图片")
    public JsonResult<Void> remove(String name){
        advertiseService.removeADImg(name);
        return JsonResult.ok();
    }

    @PostMapping("/update")
    @ApiOperation("修改广告的接口")
    public JsonResult<Void> update(AdvertiseUpdateDTO advertiseUpdateDTO){
        advertiseService.updateAD(advertiseUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/list")
    @ApiOperation("分页查询广告信息")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码",name="page",required = true,dataType = "int"),
            @ApiImplicitParam(value = "每页条数",name="pageSize",required = true,dataType = "int")
    })
    public JsonResult<JsonPage<AdvertiseVO>> selectAdvertise(Integer page,Integer pageSize){
        JsonPage<AdvertiseVO> list = advertiseService.selectAdvertise(page, pageSize);
        return JsonResult.ok(list);
    }
}